package com.gxa.goods.controller;

import com.github.pagehelper.Page;
import com.gxa.goods.service.TemplateService;
import com.gxa.luckin.core.entity.PageResult;
import com.gxa.luckin.core.entity.Result;
import com.gxa.luckin.core.entity.StatusCode;
import com.gxa.luckin.core.pojo.Template;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin//跨域
@RequestMapping("/template")
@Api(value = "模板操作==这个只有后台管理员可以操作")
public class TemplateController {


   @Resource
    private TemplateService templateService;
    //注入消息队列
    @Resource
    JmsTemplate jmsTemplate;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping("/all")
    @ApiOperation(value = "点击查看所有模板",notes = "增加会员卡信息",httpMethod = "POST",response = Map.class)
    public Result findAll(){
        List<Template> templateList = templateService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",templateList) ;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/id")
    @ApiOperation(value = "点击查看中的模板",notes = "点击查看中的模板",httpMethod = "GET",response = Map.class)
    @ApiImplicitParam(name = "id",value = "选中行的id",required = true,dataType = "int",paramType = "query")
    public Result findById(@RequestParam Integer id){
        Template template = templateService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",template);
    }


    /***
     * 新增数据//
     * @param template
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加模板--json格式",notes = "添加模板",httpMethod = "POST",response = Map.class)
    public Result add(@RequestBody @ApiParam(name = "goods",value = "模板设定的信息",required = true)Template template){
        templateService.add(template);
        //添加成功将数据添加缓存
        jmsTemplate.convertAndSend("TemplateCacheAdd",template.getId());
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param template
     * @param
     * @return
     */
    @PostMapping(value="/update")
    @ApiOperation(value = "修改模板",notes = "修改模板",httpMethod = "POST",response = Map.class)
    public Result update(@RequestBody Template template/*,@PathVariable Integer id*/){
//        template.setId(id);
        templateService.update(template);
        //修改成功后创建消息队列告诉缓存删除旧文件
        jmsTemplate.convertAndSend("TemplateCache",template.getId());
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除数据
     * @param id
     * @return
     */
     @PostMapping(value = "/delete" )
    @ApiOperation(value = "删除模板",notes = "删除模板",httpMethod = "POST",response = Map.class)
     @ApiImplicitParam(name = "id",value = "选中行的id",required = true,dataType = "int",paramType = "query")
     public Result delete(@RequestParam Integer id){
        templateService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索模板数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    @ApiOperation(value = "多条件搜索模板数据",notes = "多条件搜索模板数据",httpMethod = "GET",response = Map.class)
    public Result findList(@RequestParam Map searchMap){
        List<Template> list = templateService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/page" )
    @ApiOperation(value = "分页搜索实现",notes = "分页搜索实现",httpMethod = "GET",response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页记录数", required = true, dataType = "int", paramType = "query"),
    })
    public Result findPage(@RequestParam Map searchMap, @RequestParam  int page, @RequestParam  int size){
        Page<Template> pageList = templateService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }


}
